package com.jinyi.wwdeng.autodialer.utils

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit

/**
 * 基本数据简单存储工具
 */
object SPUtils {
    private lateinit var sharedPreferences: SharedPreferences
    /**
     * 初始化（在Application中调用一次即可）
     */
    fun init(context: Context, name: String = "app_sp") {
        sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)
    }

    // String 相关操作
    fun putString(key: String, value: String) {
        sharedPreferences.edit { putString(key, value) }
    }

    fun getString(key: String, default: String = ""): String {
        return sharedPreferences.getString(key, default) ?: default
    }

    // Int 相关操作
    fun putInt(key: String, value: Int) {
        sharedPreferences.edit { putInt(key, value) }
    }

    fun getInt(key: String, default: Int = 0): Int {
        return sharedPreferences.getInt(key, default)
    }

    // Long 相关操作
    fun putLong(key: String, value: Long) {
        sharedPreferences.edit { putLong(key, value) }
    }

    fun getLong(key: String, default: Long = 0L): Long {
        return sharedPreferences.getLong(key, default)
    }

    // Float 相关操作
    fun putFloat(key: String, value: Float) {
        sharedPreferences.edit { putFloat(key, value) }
    }

    fun getFloat(key: String, default: Float = 0f): Float {
        return sharedPreferences.getFloat(key, default)
    }

    // Boolean 相关操作
    fun putBoolean(key: String, value: Boolean) {
        sharedPreferences.edit {putBoolean(key, value)}
    }

    fun getBoolean(key: String, default: Boolean = false): Boolean {
        return sharedPreferences.getBoolean(key, default)
    }

    // 删除指定key
    fun remove(key: String) {
        sharedPreferences.edit { remove(key) }
    }

    // 清空所有数据
    fun clear() {
        sharedPreferences.edit { clear() }
    }

    // 检查key是否存在
    fun contains(key: String): Boolean {
        return sharedPreferences.contains(key)
    }
}